/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-14 下午3:21
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;


/**
 * Created by mac on 16/11/29.
 * 拨打电话确认 dialog
 */
public class CallConfirmDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private String phoneNumber; // 电话号码

    public CallConfirmDialog(Context context, String phoneNumber) {
        super(context, R.style.CustomDialog);
        this.context = context;
        this.phoneNumber = phoneNumber;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_call_confirm_cancel, null);
        setContentView(view);

        TextView tv_phone_number = (TextView) view.findViewById(R.id.tv_phone_number);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);

        tv_phone_number.setText(phoneNumber);
        btn_confirm.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);

        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 0.8);
        window.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                // 拨打电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
                try {
                    context.startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dismiss();
                break;
            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

}
